import Vue from 'vue'
import VueRouter from 'vue-router'
import LoginView from "@/views/LoginView.vue";
import HomeView from "@/views/HomeView.vue";
import UserControllerView from "@/views/UserControllerView.vue";
import MainView from "@/views/MainView.vue";

Vue.use(VueRouter)

const routes = [
    {
    path: '/',
    redirect: '/login'
    },
    {
        path: '/login',
        name: 'LoginView',
        component: LoginView
    },
  {
    path: '/HomeView',
    name: 'HomeView',
    component:HomeView,
    children:[
        {
          path: '',
          name: 'MainView',
          component: MainView
        },
        {
        path: 'UserControllerView',
        name: 'UserControllerView',
        component: UserControllerView
        }
    ]
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
    if (to.path === '/login') {
        next();
    } else {
        let token = sessionStorage.getItem('username');
        if (token === null || token === '') {
            next('/login');
        } else {
            next();
        }
    }
})
export default router
